package com.xlh.dto.courseTest;

import com.google.common.collect.Lists;
import com.xlh.pojo.courseTest.CourseTestPaperAnswerSheet;
import com.xlh.pojo.courseTest.ExperimentReport;
import com.xlh.pojo.courseTest.Practice;
import com.xlh.util.BeanUtil;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhr 2019/4/9
 */
@Data
public class GradeStatisticsListDTO {
    // 班级id
    private Long classId;
    // 班级名称
    private String className;
    // 课程状态
    private Boolean ended;
    // 课程状态中文说明
    private String endedMessage;
    // 学生id
    private Long studentId;
    // 学生姓名
    private String studentName;
    // 随堂练习
    private StatisticsDetailsDTO practice;
    // 实验报告
    private StatisticsDetailsDTO experiment;
    // 考试次数
    private Integer count;
    // 考试成绩
    private List<Integer> scoreList;
    /**
     * 用户-班级关联id
     */
    private Long userClassId;

    public void convert(
            Map<Long, String> chapterIdNameMap,
            List<Practice> practiceList,
            List<ExperimentReport> reportList,
            List<PaperAnswerSheetDTO> answerSheetList
    ) {
        List<ChapterScoreDTO> practiceScoreList = BeanUtil.batchTransform(ChapterScoreDTO.class, practiceList);
        practiceScoreList.forEach(chapterScore -> chapterScore.setChapterName(chapterIdNameMap.get(chapterScore.getChapterId())));

        List<ChapterScoreDTO> reportScoreList = BeanUtil.batchTransform(ChapterScoreDTO.class, reportList);
        reportScoreList.forEach(chapterScore -> chapterScore.setChapterName(chapterIdNameMap.get(chapterScore.getChapterId())));

        this.setPractice(new StatisticsDetailsDTO(practiceScoreList));
        this.setExperiment(new StatisticsDetailsDTO(reportScoreList));
        this.setScoreList(CollectionUtils.isEmpty(answerSheetList) ? Lists.newArrayList() :
                answerSheetList.stream().filter(paperAnswerSheetDTO -> Objects.equals(this.classId, paperAnswerSheetDTO.getClassId()))
                        .map(CourseTestPaperAnswerSheet::getScore).collect(Collectors.toList()));
        this.setCount(this.getScoreList().size());
    }

}
